package com.meilai.project.controller.web.report.helper;

import com.meilai.project.util.DateRange;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

/**
 * @author chenxing
 */
public class DateRangeHelper {
    public static DateRange calc(Integer type) {
        DateRange result = new DateRange();
        LocalDate today = LocalDate.now();
        LocalDate endDate = null;
        LocalDate beginDate = null;
        if (1 == type) {
            // 本周
            beginDate = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            endDate = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        } else if (2 == type){
            // 本月
            beginDate = today.withDayOfMonth(1);
            endDate = today.with(TemporalAdjusters.lastDayOfMonth());
        } else{
            beginDate = today.withDayOfMonth(1);
            endDate = today;
        }

        result.setBeginDate(beginDate);
        result.setEndDate(endDate);
        return result;
    }
}
